<!-- 正文开始 -->
<style>
    .companyFormPhotos {
        /* margin-left:140px; */
        line-height: 108px;
        float: left;
        margin-right: 5px;
    }

    .layui-table-cell .layui-table-sort {
        display: none;
    }
    .inspectForm .layui-form-label{
        width: 85px;
    }
    .inspectForm .layui-input-block{
        margin-left: 115px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <!-- 设备类型 -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备类型：</label>
                                    <div class="layui-input-inline">
                                        <select id='deviceType' name="deviceType"
                                                lay-filter="deviceType"
                                                class="common-select-search">
                                        </select>
                                    </div>
                                </div>
                                <!-- 设备状态 -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备状态：</label>
                                    <div class="layui-input-inline">
                                        <select id='inspectStatus' name="inspectStatus"
                                                lay-filter="inspectStatus"
                                                class="common-select-search">
                                            <option value="">请选择状态</option>
                                            <option value="1">待认领</option>
                                            <option value="2">已认领</option>
                                            <option value="3">完成</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- 设备编码 sku -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备编码：</label>
                                    <div class="layui-input-inline">
                                        <input id="deviceId" name="deviceId" class="layui-input" type="text" />
                                    </div>
                                </div>
                                <!-- 设备编码 sku -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">任务编码：</label>
                                    <div class="layui-input-inline">
                                        <input id="id" name="id" class="layui-input" type="text" />
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green"
                                            lay-filter="formSubSearchLog_main"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>
                                        重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="inspectAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="inspectTableInfo" lay-filter="inspectTableInfo"></table>
        </div>
    </div>
</div>

<script type="text/html" id="viewAttachmentHtml">
    <div id="example1" style="height: 100%;"></div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="inspectTableBar">
    <!-- 修改 只有在 刚创建+未审核 or 待领取+未审核 时才能被修改 被领取或者完成后 就不能修改了 -->
    {{# if(d.inspectStatus == 1 ) {  }}
    <a class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="edit">修改</a>
    {{# } }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <!-- 只有待认领的才能认领 并且审核通过 -->
    {{# if(d.inspectStatus ==1 && d.examineType == 0) {  }}
    <a class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="claim">认领</a>
    {{# } }}
</script>


<!-- 表单弹窗修改/添加 -->
<script type="text/html" id="inspectForm">
    <form lay-filter="inspectForm" class="layui-form inspectForm model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">任务类型：</label>
                <input id="inspectType" type="hidden" value="2"/>
                <div class="layui-input-block">
                    <div style="padding-top: 9px;">
                        <span style="color: red">任务创建</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">设备编码：</label>
                <div class="layui-input-block">
                    <input id="deviceName" name="deviceName" class="layui-input" type="hidden" />
                    <select lay-search="" id="deviceIdSel" name="deviceId" lay-filter="deviceIdSel"
                            class="common-select-search">
                    </select>
                </div>
            </div>
            <div class="layui-col-md3">
                <label class="layui-form-label">设备类型：</label>
                <div class="layui-input-block">
                    <input id="deviceTypeSelId" name="deviceTypeId" type="hidden"/>
                    <input id="deviceTypeSel" name="deviceType" type="text" class="layui-input" disabled/>
                </div>
            </div>
            <div class="layui-col-md3">
                <label class="layui-form-label">设备分类：</label>
                <div class="layui-input-block">
                    <input id="categoryId" name="categoryId" type="hidden"/>
                    <input id="categoryName" name="categoryName" type="text" class="layui-input" disabled/>
                </div>
            </div>
            <div class="layui-col-md3">
                <label class="layui-form-label">设备型号：</label>
                <div class="layui-input-block">
                    <input id="deviceModelId" name="deviceModelId" type="hidden" />
                    <input id="deviceModelName" name="deviceModelName" type="text" class="layui-input" disabled/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">设备品牌：</label>
                <div class="layui-input-block">
                    <input id="deviceBrandId" name="deviceBrandId" type="hidden" />
                    <input id="deviceBrandName" name="deviceBrandName" type="text" class="layui-input" disabled/>
                </div>
            </div>
            <div class="layui-col-md3">
                <label class="layui-form-label">设备厂商：</label>
                <div class="layui-input-block">
                    <input id="supplierId" name="supplierId" type="hidden" />
                    <input id="supplierName" name="supplierName" type="text" class="layui-input" disabled/>
                </div>
            </div>
            <div class="layui-col-md3">
                <label class="layui-form-label">计划年检时间</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="inspectTime" name="inspectTime" placeholder="计划年检时间" readonly >
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="inspectFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'tableTreeDj', 'upload', 'config','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        //初始化下拉框

        var parentId ;
        admin.reqSync('enterprise/dict/admin/dictLabel/list?page=1&limit=10&dictType=sys_device_type_code', {}, function (res) {
            if (res.code == 200) {
                parentId = res.data[0].labelValue;
            }
        });
        // 设备类型
        admin.initDataSelect('enterprise/product/admin/productType/list?page=1&limit=9999&parentId='+parentId, {},"deviceType",null,"设备类型",null,null );
        //渲染下拉
        form.render('select');

        // 渲染表格
        var insTb = table.render({
            elem: '#inspectTableInfo',
            url: config.base + 'enterprise/device/admin/inspect/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {align: 'center', type: 'numbers', title: '序号', fixed: 'left'}
                , {align: 'center', field: 'id', sort: true, title: '年检任务编号', fixed: 'left', width: '14%'}
                , {align: 'center', field: 'deviceId',sort: true, title: '设备编号', fixed: 'left'}
                , {align: 'center', field: 'deviceName',sort: true, title: '设备名称', fixed: 'left'}
                , {align: 'center', field: 'inspectCycle', sort: true, title: '年检周期'}
                , {align: 'center', field: 'inspectTime', sort: true, title: '上次年检时间'}
                , {align: 'center', field: 'doneTime', sort: true, title: '年检任务时间'}
                , {align: 'center', field: 'inspectStatusDesc', sort: true, title: '状态'}
                , {align: 'center', field: 'creatorName', sort: true, title: '创建人'}
                , {align: 'center', field: 'createTime', sort: true, title: '创建时间'}
                , {
                    align: 'center',
                    toolbar: '#inspectTableBar',
                    title: '操作',
                    width: 250,
                    fixed: 'right',
                    width: '14%'
                }
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(inspectTableInfo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data, 1);
            } else if (obj.event === 'detail') { //查看详情
                showEditModel(data ,2);
            } else if (obj.event === 'claim') { //认领
                doClaim(data);
            }
        });

        //监听排序
        table.on('sort(inspectTableInfo)', function (obj) {
            table.reload('inspectTableInfo', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog_main)', function (data) {
            data.field.status = data.field.statusSearch_main;
            data.field.postId = data.field.postIdSearchMain;
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#inspectAdd').click(function () {
            showEditModel();
        });

        //认领
        function doClaim(data) {
            layer.confirm('确定要认领吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/device/admin/inspect/claim', {id: data.id}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'POST');
            });
        }

        //审核
        function doSHModel(data) {
            admin.open({
                type: 1,
                area: ['500px', '500px'],
                offset: '65px',
                title: '维保任务审核',
                content: $('#inspectSHForm').html(),
                success: function () {
                    layui.form.render('select');
                    form.on('submit(inspectShFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.id = data.id;
                        admin.req('enterprise/device/admin/inspect/audit', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('inspectTableInfo');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                                layer.closeAll('page');
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }

        // 显示编辑弹窗
        function showEditModel(data, type) {
            admin.open({
                type: 1,
                area: '1200px',
                offset: '65px',
                title: data ? '修改年检任务' : '添加年检任务',
                content: $('#inspectForm').html(),
                success: function () {
                    if (type != 2) {
                        laydate.render({
                            elem: '#inspectTime'
                            , type: 'datetime'
                        });
                    }
                    admin.reqSync('enterprise/device/admin/device/list', {limit: 9999}, function (res) {
                        if (res.code === 200) {
                            //设备编码
                            loadSelectHtml(res.data, "请选择设备型号", "deviceId", '[lay-filter="inspectForm"]', 3);
                        }
                    }, 'get');
                    //设置默认值
                    form.render();
                    form.val('inspectForm', data);
                    //选择框事件
                    form.on('select(deviceIdSel)', function (data) {

                        let dataObj = JSON.parse($(data.elem[data.elem.selectedIndex]).attr('data'));
                        //拿到值
                        $("#deviceTypeSel").val(dataObj.deviceTypeName);
                        $("#deviceName").val(dataObj.deviceName);
                        $("#deviceTypeSelId").val(dataObj.deviceType);
                        $("#categoryId").val(dataObj.categoryId);
                        $("#categoryName").val(dataObj.categoryName);
                        $("#deviceModelId").val(dataObj.deviceModelId);
                        $("#deviceModelCode").val(dataObj.deviceModelCode);
                        $("#deviceModelName").val(dataObj.deviceModelName);
                        $("#deviceBrandId").val(dataObj.deviceBrandId);
                        $("#deviceBrandName").val(dataObj.deviceBrandName);
                        $("#supplierId").val(dataObj.supplierId);
                        $("#supplierName").val(dataObj.deviceBrandName);
                    });
                    if (type === 2) {
                        $('form[lay-filter="inspectForm"] input').attr('readonly', 'readonly');
                        $('form[lay-filter="inspectForm"] select').attr('disabled', 'disabled');
                        $('button[lay-filter="inspectFormSubmit"]').remove();
                        form.render('select');
                    }
                    // 表单提交事件
                    form.on('submit(inspectFormSubmit)', function (d) {
                        layer.load(2);
                        console.log(d.field);
                        let params = {
                            ...d.field,
                        }
                        //新增修改
                        admin.req(data ? 'enterprise/device/admin/inspect/update' : 'enterprise/device/admin/inspect/add', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('inspectTableInfo');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            });


            /**
             * 加载地址HTML
             * @param data 下拉菜单数据
             * @param tips select 默认提示
             * @param htmltarget html要插入的位置
             * @param name  下拉框需要显示值的属性
             */
            function loadSelectHtml(data, tips, htmltarget, formName, type) {
                let html = ' <option value>' + tips + '</option>';
                if (!data || data == null) {
                    $("form" + formName + " select[name='" + htmltarget + "']").html(html);
                    form.render('select');
                    return;
                }
                for (let i = 0; i < data.length; i++) {
                    if (type == 1) {
                        html += `<option value='${data[i].id}' data='${JSON.stringify(data[i])}'>${data[i].deviceCode}</option>`;
                    } else if (type == 2) {
                        html += `<option value='${data[i].labelValue}' data='${JSON.stringify(data[i])}'>${data[i].labelName}</option>`;
                    } else if (type == 3) {
                        html += `<option value='${data[i].id}' data='${JSON.stringify(data[i])}'>${data[i].name}</option>`;
                    }
                }
                $("form" + formName + " select[name='" + htmltarget + "']").html(html);
                form.render('select');
            }
        }
    });
</script>